﻿@using Frogsoft.Permission.Dtos
@implements IDialogContentComponent<AppUserDto>
@inject Frogsoft.Permission.UserService UserService
@rendermode InteractiveServer
@attribute [Authorize(Policy = "system_permission_user")]

<FluentGrid Spacing="3">
    <FluentGridItem xs="12" sm="6">
        <FluentTextField @bind-Value="newUser.UserName"
                         Label="用户名"
                         Style="width: 100%"
                         Required />
    </FluentGridItem>
    <FluentGridItem xs="12" sm="6">
        <FluentTextField @bind-Value="newUser.FullName"
                         Label="姓名"
                         Style="width: 100%" />
    </FluentGridItem>
    <FluentGridItem xs="12" sm="6">
        <FluentTextField @bind-Value="newUser.Email"
                         Label="邮箱"
                         Type="email"
                         Style="width: 100%"
                         Required />
    </FluentGridItem>
    <FluentGridItem xs="12" sm="6">
        <FluentTextField @bind-Value="newUser.Mobile"
                         Label="手机号"
                         Style="width: 100%" />
    </FluentGridItem>
    <FluentGridItem xs="12" sm="6">
        <FluentTextField @bind-Value="newUser.Password"
                         Label="密码"
                         Type="password"
                         Style="width: 100%"
                         Required />
    </FluentGridItem>
</FluentGrid>
<!-- Footer -->
<FluentDialogFooter>
    <FluentButton Appearance="Appearance.Accent"
                  OnClick="@SaveAsync">
        保存
    </FluentButton>
    <FluentButton OnClick="@CancelAsync">
        取消
    </FluentButton>
</FluentDialogFooter>

@code {
    [CascadingParameter]
    public FluentDialog? Dialog { get; set; }

    [Parameter]
    public AppUserDto Content { get; set; } = default!;

    private AppUserDto newUser = new AppUserDto();

    private async Task SaveAsync()
    {
        try
        {
            var result = UserService.Create(newUser);
            if (result != null)
            {
                await Dialog!.CloseAsync(DialogResult.Ok(result));
            }
        }
        catch (Exception ex)
        {
            // 可以在这里添加错误处理逻辑
            await Dialog!.CloseAsync(DialogResult.Cancel());
        }
    }

    private async Task CancelAsync()
    {
        await Dialog.CancelAsync();
    }
        

    protected override void OnInitialized()
    {
        
    }
}
